// This File Command script gives examples of how to send HS packets
// and HS bursts with various errors.  The script is written generally
// to work with any bit-rate and lane count and the packet data used
// is easily changed.  For a specific bit-rate, lane count, and packet
// data, the script can be greatly simplified.

# FILE "../DSIDataTypes.txt"

// Define subroutine to send HS burst given BurstData buffer that includes
// HSZero, HSSync, packet data, HSTrail bytes
# SUB SendHSBurst burstData

	// send HS burst entry 
	# LP_STATES ACT: 3 1 
	
	// send HSPrepare for 100 ns
	# LP_STATES ACT 100: 0
	
	// send HS data
	# HS_BYTES DEMUX
	# STREAM burstData
	
	# LP_STATES ACT: 3
# ENDSUB

// compute how many UI in 200 ns 
// then truncate UI200 to compute number of HSZero bytes to use
# UI200 = 200000 / SYS_UI_PS
# HSZeroCnt = UI200 / 8

// compute how many UI in 100 ns
// then compute number of HSTrail bytes to use (~= 8 UI + 100 ns)
# UI100 = UI200 / 2
# HSTrailCnt = 1 + (UI100 + 7) / 8

// build buffer with generic long write packet
# BUF pkt: DT_GENERIC_LONG_WRITE -4 -1 1 2 3 4 5 -2

// get last packet bytes (to compute HSTrail data)
# BUF trailBytes
# STREAM pkt (length(pkt)-SYS_LANE_CNT) SYS_LANE_CNT

// compute HSTrail data (invert msb and convert to byte)
# i = 0
# LOOP_START SYS_LANE_CNT
	# trailBytes[i] = (1 - (trailBytes[i] >> 7)) * ffh
	# i = i + 1
# LOOP_END

// build buffer with HSZero, HSSync, generic long write packet, HSTrail
# BUF HSBurst
*(HSZeroCnt * SYS_LANE_CNT) 0
*SYS_LANE_CNT b8h
# STREAM pkt
# LOOP_START HSTrailCnt
# STREAM trailBytes
# LOOP_END

// finally, add extra HSTrail bytes to make all HS lanes have equal byte count
# extraTrail = SYS_LANE_CNT - (length(pkt) % SYS_LANE_CNT)
# IF (extraTrail < SYS_LANE_CNT)
	# STREAM trailBytes 0 extraTrail
# ENDIF

// for convenience, define position variables for fields in HSBurst
# syncPos = HSZeroCnt * SYS_LANE_CNT
# hdrPos = syncPos + SYS_LANE_CNT
# payloadPos = hdrPos + 4
# trailPos = hdrPos + length(pkt)
# crcPos = trailPos - 2
# IF (extraTrail < SYS_LANE_CNT)
	# lane0TrailPos = trailPos + extraTrail
# ELSE
	# lane0TrailPos = trailPos
# ENDIF

/////////////////////////////////////////////////////////////////

// send valid HSBurst 
# CALL SendHSBurst HSBurst

// send HSBurst with single-bit error in lane0 HSSync 
# BUF errHSBurst
# STREAM HSBurst
# errHSBurst[syncPos] = errHSBurst[syncPos] ^ 20h
# CALL SendHSBurst errHSBurst

// send HSBurst with two-bit error in ECC
# BUF errHSBurst
# STREAM HSBurst
# errHSBurst[hdrPos+3] = errHSBurst[hdrPos+3] ^ 5h
# CALL SendHSBurst errHSBurst

// send HSBurst with single-bit error in payload byte 2
# BUF errHSBurst
# STREAM HSBurst
# errHSBurst[payloadPos+2] = errHSBurst[payloadPos+2] ^ 1h
# CALL SendHSBurst errHSBurst

// send HSBurst with single-bit error in CRC lsb
# BUF errHSBurst
# STREAM HSBurst
# errHSBurst[crcPos] = errHSBurst[crcPos] ^ 80h
# CALL SendHSBurst errHSBurst

// send HSBurst with invalid HSTrail in lane 0
# BUF errHSBurst
# STREAM HSBurst
# errHSBurst[lane0TrailPos] = errHSBurst[lane0TrailPos] ^ 6h
# CALL SendHSBurst errHSBurst

// send HSBurst with EOT Sync error (1 extra bit)
// (for simplicity, we set the extra 1 bit to the msb of the last packet byte)
# BUF errHSBurst
# STREAM HSBurst
# errHSBurst[trailPos] = errHSBurst[trailPos] ^ 1h
# CALL SendHSBurst errHSBurst

// send HSBurst with EOT Sync error (6 extra bits)
// (for simplicity, we set the extra 6 bits to the msb of the last packet byte)
# BUF errHSBurst
# STREAM HSBurst
# errHSBurst[trailPos] = errHSBurst[trailPos] ^ 3fh
# CALL SendHSBurst errHSBurst

